<template>
  <div
    id="search-tips"
    :class="{ 'is-show-search': isShowSearch }"
    @mousedown="stillShow">
    <my-scrollbar ref="scrollRef">
      <div class="title">热搜榜</div>
      <div
        class="hots-item"
        v-for="(item, index) in hots"
        :key="index"
        @click="hotsSearch(item.searchWord)">
        <div class="number">{{ index + 1 }}</div>
        <div class="context">
          <div class="first-line">
            <div class="search-word">{{ item.searchWord }}</div>
            <div class="score">{{ item.score }}</div>
            <img class="icon" :src="item.iconUrl" alt="">
          </div>
          <div class="second-line">
            {{ item.content }}
          </div>
        </div>
      </div>
    </my-scrollbar>
    <div class="triangle"></div>
  </div>
</template>

<script>
import MyScrollbar from 'components/content/perfectscrollbar/MyScrollbar.vue'

export default {
  name: 'SearchTips',
  data () {
    return {
      hots: null
    }
  },
  components: {
    MyScrollbar
  },
  props: {
    isShowSearch: {
      type: Boolean,
      default () {
        return false
      }
    }
  },
  created () {
    this._getTopSearch()
  },
  methods: {
    // 1. 获得热点关键字
    async _getTopSearch () {
      const result = await this.$http.get('/search/hot/detail')
      if (result.status !== 200 || result.data.code !== 200) return this.$message.info('获取热搜榜失败')
      // 获取成功
      // console.log(result)
      this.hots = result.data.data
    },
    // 2. 点击热点关键字搜索
    hotsSearch (searchWord) {
      // 关闭搜索框
      this.$emit('hiddenSearch', 'close')
      // 如果query是相同的则取消搜索
      if (this.$route.query.keyword === searchWord) return false
      this.$router.push(`/search/results?keywords=${searchWord}`)
    },
    // 3. 点击关联区域仍然显示
    stillShow () {
      this.$emit('stillShow')
    },
    // 4. 刷新滚动条
    scrollUpdate () {
      setTimeout(() => {
        this.$refs.scrollRef.update()
      }, 0)
    }
  }
}
</script>

<style scoped lang="less">
#search-tips {
  user-select: none;
  position: relative;
  top: 15px;
  left: -8px;
  width: 170%;
  height: 480px;
  display: none;
  background-color: #fff;
  box-shadow: 0 0 2px 0 rgba(0, 0, 0, 0.5);
  background-color: #fafafa;
  z-index: 999999;
  transition: all 500ms;
  .title {
    padding-left: 20px;
    // margin: 15px 0;
    margin-top: 10px;
    margin-bottom: 15px;
    line-height: 30px;
    color: #666;
    font-size: 16px;
  }
  .hots-item {
    cursor: pointer;
    padding-left: 20px;
    display: flex;
    height: 60px;
    &:hover {
      background-color: #ededed;
    }
    .number {
      line-height: 60px;
      color: #ff3a3a;
      font-size: 18px;;
    }
    .context {
      padding-left: 30px;
      height: 100%;
      .first-line {
        height: 50%;
        display: flex;
        align-items: center;
        .search-word {
          color: #333;
          font-weight: 700;
          font-size: 13px;
        }
        .score {
          padding-left: 15px;
          color: #cdcdcd;
          font-size: 13px;
        }
        .icon {
          padding-left: 10px;
          height: 50%;
        }
      }
      .second-line {
        font-size: 13px;
        color: #999;
      }
    }
  }
  .triangle {
    position: absolute;
    width: 15px;
    height: 15px;
    background-color: #fff;
    z-index: 1001;
    transform: rotateZ(45deg);
    top: -5px;
    left: 20px;
  }
 &.is-show-search {
  display: block;
 }
}
</style>
